package com.iaz.tech.tools.common.utils;

import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.HashMap;
import java.util.Map;
import java.util.NoSuchElementException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 

/**
 * AZChainTools 链表工具, 用于实现双向链表
 * 
 * @param <T>
 */
@EqualsAndHashCode(callSuper = true)
@Data
public class DoublyLinkedList<T> extends AZChain<T> implements IChainListAble<T>, IChainListIteratorAble<T> {
	private final static Logger logger = (Logger) LoggerFactory.getLogger(DoublyLinkedList.class);
	/**
	 * 链表头
	 */
	private IChainNodeAble<T> header = null;
	/**
	 * 链表尾部
	 */
	private IChainNodeAble<T> tail = null;
	/**
	 * 链表游标
	 */
	private IChainNodeAble<T> cursor = null;
	/* 当前层 */
	private int size;

	// 构造函数
	public DoublyLinkedList() {
		this.header = null;
		this.tail = null;
		this.size = 0;
	}

	// 判断链表是否为空
	public boolean isEmpty() {
		return (this.size() == 0);
	}

	// 在链表头部添加元素
	public void addFirst(T data) {
		try {
			this.insertHeader(data);
			size++;
		} catch (Exception e) {
			logger.error("addFirst 执行异常，错误信息：" + e.getMessage());
		}
	}

	// 在链表尾部添加元素
	public void addLast(T data) {
		try {
			this.appendTail(data);
			size++;
		} catch (Exception e) {
			logger.error("addLast 执行异常，错误信息：" + e.getMessage());
		}
	}

	// 在指定位置插入元素
	public void add(int index, T data) {

		try {
			int cur_size = this.size();
			if (index < 0 || index > cur_size) {
				throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
			}

			if (index == 0) {
				addFirst(data);
			} else if (index == size) {
				addLast(data);
			} else {
				this.insert(index, data);
				this.size++;
			} 
		} catch (Exception e) {
			logger.error("add 执行异常，错误信息：" + e.getMessage());
		}
	}

	// 移除头部元素
	public T removeFirst() {
		T result = null;
		try {
			if (isEmpty()) {
				throw new NoSuchElementException("List is empty");
			}

			this.first();
			result = this.delete();
			size--;
		} catch (Exception e) {
			logger.error("removeFirst 执行异常，错误信息：" + e.getMessage());
			result = null;
		}
		return result;
	}

	// 移除尾部元素
	public T removeLast() {
		T result = null;
		try {
			if (isEmpty()) {
				throw new NoSuchElementException("List is empty");
			}

			this.end();
			result = this.delete();
			size--;
		} catch (Exception e) {
			logger.error("removeLast 执行异常，错误信息：" + e.getMessage());
			result = null;
		}
		return result;
	}

	// 移除指定位置的元素
	public T remove(int index) {
		try {
			if (index < 0 || index >= size) {
				throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
			}
			if (index == 0) {
				return removeFirst();
			} else if (index == size - 1) {
				return removeLast();
			} else {
				return this.delete(index);
			}
		} catch (Exception e) {
			logger.error("remove Index 执行异常，错误信息：" + e.getMessage());
		}
		return null;
	}

	// 获取指定位置的元素
	public T get(int index) {
		Map<Long, Integer> _tmp_map = new HashMap<Long, Integer>();
		try {
			if (index < 0 || index > size) {
				throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
			}
			if (this.isEmpty()) {
				return null;
			}

			IChainNodeAble<T> _tmp_cursor = this.header;
			if (_tmp_cursor == null) {
				throw new Exception("链表异常，header 没有指定。");
			}
			int count = 0;
			while ((_tmp_cursor != null) && (_tmp_cursor.nextChain() != null)) {
				if (count >= index) {
					break;
				}
				if (_tmp_map.containsKey(_tmp_cursor.chainIdentifyNo())) {
					throw new Exception("Schedule Chians 不支持循环链表。");
				}

				_tmp_cursor = _tmp_cursor.nextChain();
				_tmp_map.put(this.cursor.chainIdentifyNo(), 1);
				count++;
			}

			return _tmp_cursor.toData();
		} catch (Exception e) {
			logger.error("get 执行异常，错误信息：" + e.getMessage());
		} finally {
			_tmp_map.clear();
		}
		return null;
	}

	// 设置指定位置的元素
	public T set(int index, T data) {
		try {
			if (index < 0 || index >= size) {
				throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
			}
			if (this.isEmpty()) {
				return null;
			}
			this.skip(index);
			T oldData = this.cursor.toData();
			/* 设置 */
			this.cursor.pushData(data);
			return oldData;
		} catch (Exception e) {
			logger.error("set 执行异常，错误信息：" + e.getMessage());
		}
		return null;
	}

	// 获取指定位置的节点
	public IChainNodeAble<T> getChain(int index) {
		try {
			if (index < 0 || index >= size) {
				throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
			}
			if (this.isEmpty()) {
				return null;
			}
			this.skip(index);
			return this.cursor;
		} catch (Exception e) {
			logger.error("get 执行异常，错误信息：" + e.getMessage());
		}
		return null;
	}

	@Override
	public IChainNodeAble<T> reset() {
		try {
			this.first();
			return this.cursor;
		} catch (Exception e) {
			logger.error("reset 执行异常，错误信息：" + e.getMessage());
		}
		return null;
	}

	/**
	 * 在当前链表Cursor增加
	 * 
	 * @param data
	 */
	@SuppressWarnings("unused")
	@Override
	public IChainNodeAble<T> append(T _new_chain_data) {
		IChainNodeAble<T> result = null;
		try {
			if (_new_chain_data == null) {
				throw new Exception("入口参数 _new_chian_data 没有指定！");
			}
			IChainNodeAble<T> _new_chain_node = new AZChain<T>(_new_chain_data);
			if (this.isEmpty()) {
				this.header = _new_chain_node;
				this.tail = _new_chain_node;
				this.cursor = _new_chain_node;
				return this.cursor;
			}

			result = this.cursor;
			if (_new_chain_node == null) {
				throw new RuntimeException("实例化AZChain 失败！");
			}
			/* 将新成员加入链表 */
			_new_chain_node.putNext(result.nextChain());
			if (result.nextChain() != null) {
				result.nextChain().putPrev(_new_chain_node);
			}

			result.putNext(_new_chain_node);
			_new_chain_node.putPrev(result);
			/* 移动游标在新的链表成员 */
			this.cursor = _new_chain_node;
			
			if ( this.cursor.prevChain() == null ) {
				this.header = this.cursor;
			}
			if (this.cursor.nextChain() == null ) {
				this.tail = this.cursor;
			}
			
			//this.size++;
		} catch (Exception e) {
			logger.error("执行 append 异常，错误信息：" + e.getMessage());
		}
		return result;
	}

	@Override
	public IChainNodeAble<T> append(long _post, T _new_chain_data) {
		IChainNodeAble<T> result = null;
		try {
			IChainNodeAble<T> _cursor = this.skip(_post);
			if (_cursor == null) {
				throw new Exception("跳转出现异常：出现空的指针！");
			}
			/* 在指定位置后，添加 */
			result = this.append(_new_chain_data);
		} catch (Exception e) {
			logger.error("执行 append POST 异常，错误信息：" + e.getMessage());
		}
		return result;
	}

	/**
	 * 在末尾增加
	 */
	@Override
	public IChainNodeAble<T> appendTail(T _new_chain_data) {
		IChainNodeAble<T> result = null;
		try {
			this.end();
			/* 在指定位置后，添加 */
			this.append(_new_chain_data);
		} catch (Exception e) {
			logger.error("执行 appendTail 异常，错误信息：" + e.getMessage());
		}
		return result;
	}

	@Override
	public IChainNodeAble<T> appendHeader(T _new_chain_data) {
		IChainNodeAble<T> result = null;
		try {
			this.first();
			/* 在指定位置后，添加 */
			result = this.append(_new_chain_data);
		} catch (Exception e) {
			logger.error("执行 appendHeader 异常，错误信息：" + e.getMessage());
		}
		return result;
	}

	@SuppressWarnings("unused")
	@Override
	public IChainNodeAble<T> insert(T _new_chain_data) {
		IChainNodeAble<T> result = null;
		try {
			if (_new_chain_data == null) {
				throw new Exception("入口参数 _new_chian_data 没有指定！");
			}

			result = new AZChain<T>(_new_chain_data);
			if (this.isEmpty()) {
				this.header = result;
				this.tail = result;
				this.cursor = result;
				return this.cursor;
			}

			if (this.cursor == null) {
				throw new Exception("链表为空或者游标没有指定！");
			}

			if (this.cursor.prevChain() != null) {
				this.cursor.prevChain().putNext(result);
			}
			result.putPrev(this.cursor.prevChain());

			result.putNext(this.cursor);
			this.cursor.putPrev(result);
			this.cursor = result;
			
			if ( this.cursor.prevChain() == null ) {
				this.header = this.cursor;
			}
			if (this.cursor.nextChain() == null ) {
				this.tail = this.cursor;
			}

		} catch (Exception e) {
			logger.error("执行 insert 异常，错误信息：" + e.getMessage());
		}
		return result;
	}

	@Override
	public IChainNodeAble<T> insert(long _post, T _new_chain_data) {
		IChainNodeAble<T> result = null;
		try {
			this.skip(_post);

			/* 在指定位置后，添加 */
			result = this.insert(_new_chain_data);
		} catch (Exception e) {
			logger.error("执行 insert POST 异常，错误信息：" + e.getMessage());
		}
		return result;
	}

	@Override
	public IChainNodeAble<T> insertHeader(T _new_chain_data) {
		IChainNodeAble<T> result = null;
		try {
			this.first();
			/* 在指定位置后，添加 */
			result = this.insert(_new_chain_data);
		} catch (Exception e) {
			logger.error("执行 insertHeader 异常，错误信息：" + e.getMessage());
		}
		return result;
	}

	@Override
	public IChainNodeAble<T> insertTail(T _new_chain_data) {
		IChainNodeAble<T> result = null;
		try {
			this.end();
			/* 在指定位置后，添加 */
			this.insert(_new_chain_data);
		} catch (Exception e) {
			logger.error("执行 insertTail 异常，错误信息：" + e.getMessage());
		}
		return result;
	}

	/**
	 * 非循环列表，并避免生成循环列表, 避免无限循环
	 */
	public IChainNodeAble<T> first() {
		/* 避免循环 */
		Map<Long, Integer> _tmp_map = new HashMap<Long, Integer>();
		try {
			if (this.isEmpty()) {
				return this.cursor;
			}
			/* 临时认证 */
			_tmp_map.put(this.cursor.chainIdentifyNo(), 1);
			while ((this.cursor != null) && (this.cursor.prevChain() != null)) {
				this.cursor = this.cursor.prevChain();
				if (_tmp_map.containsKey(this.cursor.chainIdentifyNo())) {
					throw new Exception("Schedule Chians 不支持循环链表。");
				}
				_tmp_map.put(this.cursor.chainIdentifyNo(), 1);
			}
			if (this.cursor == null) {
				throw new Exception("链表异常，在遍历过程中出现吊链问题。");
			}
			this.header = this.cursor;
			return this.cursor;
		} catch (Exception e) {
			logger.error("执行first出现错误：" + e.getMessage());
		} finally {
			_tmp_map.clear();
		}
		return null;
	}

	/**
	 * 非循环列表，并避免生成循环列表 避免无限循环
	 */
	@Override
	public IChainNodeAble<T> end() {
		/* 避免循环 */
		Map<Long, Integer> _tmp_map = new HashMap<Long, Integer>();
		try {
			if (this.isEmpty()) {
				return this.cursor;
			}
			/* 临时认证 */
			_tmp_map.put(this.cursor.chainIdentifyNo(), 1);
			while ((this.cursor != null) && (this.cursor.nextChain() != null)) {
				this.cursor = this.cursor.nextChain();
				if (_tmp_map.containsKey(this.cursor.chainIdentifyNo())) {
					throw new Exception("Schedule Chians 不支持循环链表。");
				}
				_tmp_map.put(this.cursor.chainIdentifyNo(), 1);
			}
			if (this.cursor == null) {
				throw new Exception("链表异常，在遍历过程中出现吊链问题。");
			}
			/* 整理 */
			this.tail = this.cursor;
			return this.cursor;
		} catch (Exception e) {
			logger.error("执行 end 出现错误：" + e.getMessage());
		} finally {
			_tmp_map.clear();
		}
		return null;
	}

	/**
	 * 根据指定_post 跳跃
	 */
	@Override
	public IChainNodeAble<T> skip(long _post) {
		/* 避免循环 */
		Map<Long, Integer> _tmp_map = new HashMap<Long, Integer>();
		try {
			long step_count = Math.abs(_post);
			/* 临时认证 */
			_tmp_map.put(this.cursor.chainIdentifyNo(), 1);
			int count = 0;
			while (this.cursor != null) {
				if (count >= step_count) {
					break;
				}
				if (_post > 0) {
					if (this.cursor.nextChain() == null) {
						break;
					}
					this.cursor = this.cursor.nextChain();
				} else if (_post < 0) {
					if (this.cursor.prevChain() == null) {
						break;
					}
					this.cursor = this.cursor.prevChain();
				}
				if (_tmp_map.containsKey(this.cursor.chainIdentifyNo())) {
					throw new Exception("Schedule Chians 不支持循环链表。");
				}
				_tmp_map.put(this.cursor.chainIdentifyNo(), 1);
				count++;
			}
			if (this.cursor == null) {
				throw new Exception("链表异常，在遍历过程中出现吊链问题。");
			}
			return this.cursor;
		} catch (Exception e) {
			logger.error("调用 skip 异常，错误信息：" + e.getMessage());
		} finally {
			_tmp_map.clear();
		}
		return null;
	}
	
	@Override
	public IChainNodeAble<T> position(long position) {
		if (position < 0) {
			return null;
		}
		this.first();
		return this.skip(position);
	}

	@Override
	public int size() {
		this.size = 0;
		Map<Long, Integer> _tmp_map = new HashMap<Long, Integer>();
		try {
			if (this.header == null) {
				return this.size;
			}

			if (this.tail == null) {
				return this.size;
			}

			IChainNodeAble<T> step_cursor = this.cursor;
			while (step_cursor.prevChain() != null) {
				if (_tmp_map.containsKey(step_cursor.chainIdentifyNo())) {
					throw new Exception("Schedule Chians 不支持循环链表。");
				}
				_tmp_map.put(step_cursor.chainIdentifyNo(), 1);
				step_cursor = step_cursor.prevChain();
			}
			_tmp_map.clear();
			this.size = 1;
			while (step_cursor.nextChain() != null) {
				if (_tmp_map.containsKey(step_cursor.chainIdentifyNo())) {
					throw new Exception("Schedule Chians 不支持循环链表。");
				}
				_tmp_map.put(step_cursor.chainIdentifyNo(), 1);
				step_cursor = step_cursor.nextChain();
				this.size++;
			}

			return this.size;
		} catch (Exception e) {
			logger.error("调用 skip 异常，错误信息：" + e.getMessage());
			this.size = 0;
		} finally {
			_tmp_map.clear();
		}
		return this.size;
	}

	/**
	 * 删除, 从cursor
	 */
	@Override
	public T delete() {
		T result = null;
		try {
			if (this.isEmpty()) {
				return result;
			}
			result = this.cursor.toData();
			IChainNodeAble<T> _tmp_cusor = this.cursor;
			if (this.cursor.nextChain() != null) {
				_tmp_cusor = this.cursor.nextChain();
			} else if (this.cursor.prevChain() != null) {
				_tmp_cusor = this.cursor.prevChain();
			}

			if (this.cursor.prevChain() != null) {
				this.cursor.prevChain().putNext(this.cursor.nextChain());
			}
			if (this.cursor.nextChain() != null) {
				this.cursor.nextChain().putPrev(this.cursor.prevChain());
			}

			/* 删除为子连接 */
			if (this.cursor.upperChain() != null) {
				this.cursor.upperChain().putDown(_tmp_cusor);
				_tmp_cusor.putUpper(this.cursor.upperChain());
			}
			
			if ((this.cursor.prevChain() == null) &&
					(this.cursor.nextChain() == null) ) {
				/* 最后一个成员, 该链表为空*/
				this.header = null;
				this.tail = null;
				this.cursor = null;
				return result;
			} else {
				this.cursor.putPrev(null);
				this.cursor.putNext(null);
				this.cursor.putDown(null);
				this.cursor.putUpper(null);
			}

			this.cursor = _tmp_cusor;
			if (this.cursor == null) {
				return result;
			}

			if (this.cursor.prevChain() == null) {
				this.header = this.cursor;
			}
			if (this.cursor.nextChain() == null) {
				this.tail = this.cursor;
			}

		} catch (Exception e) {
			logger.error("执行 delete 异常，错误信息：" + e.getMessage());
		}
		return result;
	}

	/**
	 * 删除指定便宜位置
	 */
	@Override
	public T delete(int post) {
		T result = null;
		try {
			this.position(post);
			result = this.delete();
		} catch (Exception e) {
			logger.error("执行 delete POST 异常，错误信息：" + e.getMessage());
			result = null;
		}
		return result;
	}

	@Override
	public void clear() {
		try {
			this.first();
			if (this.cursor == null) {
				return;
			}
			while ((this.cursor != null) && (this.cursor.nextChain() != null)) {
				if (this.cursor.containerChildren()) {
					this.cursor.clear();
				}
				
				this.delete();
				/* 移动指针到最 */
				this.first();
			}
			this.header = null;
			this.tail = null;
			this.cursor = null;
		} catch (Exception e) {
			logger.error("执行 clear 异常，错误信息：" + e.getMessage());
		}
	}

	@Override
	public boolean hasCursorNext() {
		return (this.cursor.nextChain() != null);
	}
	/**
	 * 进行执行
	 */
	@SuppressWarnings("unchecked")
	@Override
	public IChainNodeAble<T> nextIndexNode() {
		IChainNodeAble<T> result = null;
		try {
			
			if (this.cursor.containerChildren()) {
				if (((IChainListIteratorAble<T>)this.cursor).isInnerTail()) {
					/* 返回, 并将游标重置 */
					this.cursor.reset();
					this.cursor = this.cursor.nextChain();
					result = this.cursor;					
					//this.cursor = this.header;
					return result;
				}
				return ((IChainListIteratorAble<T>)this.cursor).nextIndexNode(); 
			} else {
				result = this.cursor;
				this.cursor = this.cursor.nextChain();				
				return result;
			}
			
		} catch (Exception e) {
			logger.error("执行 next 异常，错误信息：" + e.getMessage()); 
		}
		return result;
	}
	
	
	
	
	@Override
	public void clearToLast(IChainNodeAble<T> _cursor) { 
		try {
			
		} catch (Exception e) {
			logger.error("执行 clearToLast 异常，错误信息：" + e.getMessage()); 
		}	
	}

	@Override
	public IChainNodeAble<T> firstForCursor(IChainNodeAble<T> _cursor) {
		IChainNodeAble<T> result = null;
		Map<Long, Integer> _tmp_map = new HashMap<Long, Integer>();
		try {
			if (_cursor == null) {
				throw new java.lang.IllegalArgumentException("入口参数_cursor没有指定或者为null。");
			}
			result = _cursor;
			/* 临时认证 */
            _tmp_map.put(result.chainIdentifyNo(), 1);
			while((result != null) && (result.prevChain() != null)) {
				if (_tmp_map.containsKey(this.cursor.chainIdentifyNo())) {
                    throw new Exception("在DoublyLinkedList中Chians 不支持循环链表。");
                }
				result = result.prevChain();
				_tmp_map.put(result.chainIdentifyNo(), 1);
			}
			
		} catch (Exception e) {
			logger.error("执行 firstForCursor 异常，错误信息：" + e.getMessage()); 
			result = null;
		} finally {
            _tmp_map.clear();
        }
		return result;
	}

	@Override
	public IChainNodeAble<T> tailForCursor(IChainNodeAble<T> _cursor) {
		IChainNodeAble<T> result = null;
		Map<Long, Integer> _tmp_map = new HashMap<Long, Integer>();
		try {
			if (_cursor == null) {
				throw new java.lang.IllegalArgumentException("入口参数_cursor没有指定或者为null。");
			}
			result = _cursor;
			/* 临时认证 */
            _tmp_map.put(result.chainIdentifyNo(), 1);
			while((result != null) && (result.nextChain() != null)) {
				if (_tmp_map.containsKey(this.cursor.chainIdentifyNo())) {
                    throw new Exception("在DoublyLinkedList中Chians 不支持循环链表。");
                }
				result = result.nextChain();
				_tmp_map.put(result.chainIdentifyNo(), 1);
			}
			
		} catch (Exception e) {
			logger.error("执行 firstForCursor 异常，错误信息：" + e.getMessage()); 
			result = null;
		} finally {
            _tmp_map.clear();
        }
		return result;
	}

	@Override
	public IChainNodeAble<T> positionForCursor(IChainNodeAble<T> _cursor, int position) {
		IChainNodeAble<T> result = null;
		Map<Long, Integer> _tmp_map = new HashMap<Long, Integer>();
		try {
			/* 迁移到最开始 */
			IChainNodeAble<T> _t_cursor_index = this.firstForCursor(_cursor);
			if (_t_cursor_index == null) {
				throw new java.lang.NullPointerException("链表值异常，其中Header链表异常。");
			}
			if (position < 0) {
				throw new java.lang.IllegalArgumentException("入口参数position不能小于0的值");
			}
			
			 _tmp_map.put(_t_cursor_index.chainIdentifyNo(), 1);
			int count = 0;
			while((_t_cursor_index!= null) && (_t_cursor_index.nextChain() != null)) {
				if(count>= position) {
					break;
				}
				if (_tmp_map.containsKey(this.cursor.chainIdentifyNo())) {
                    throw new Exception("在DoublyLinkedList中Chians 不支持循环链表。");
                }
				_t_cursor_index = _t_cursor_index.nextChain();
				_tmp_map.put(_t_cursor_index.chainIdentifyNo(), 1);
			}
			
			result = _t_cursor_index;
			
		} catch (Exception e) {
			logger.error("执行 positionForCursor 异常，错误信息：" + e.getMessage()); 
		} finally {
            _tmp_map.clear();
        }
		return result;
	}
	
	/**
	 * 已经到尾部了
	 * @return
	 */
	@Override
	public boolean isInnerTail() {
		return (this.cursor == null) ;
	}
	
	@Override
	public boolean containerChildren() {
		return true;
	}

	@Override
	public IChainNodeAble<T> nextCursor() {
		IChainNodeAble<T> result = null; 
		try {
			if (this.isEmpty()) {
				return result;
			}
			if (this.isCursorTail()) {
				return result;
			}
			if (this.cursor == null) {
				throw new Exception("链表结构异常，其中游标为空。");
			}
			this.cursor = this.cursor.nextChain();
			result = this.cursor;
		} catch (Exception e) {
			logger.error("执行 nextCursor 异常，错误信息：" + e.getMessage()); 
		} 
		return result;
	}

	@Override
	public IChainNodeAble<T> prevCursor() {
		IChainNodeAble<T> result = null; 
		try {
			if (this.isEmpty()) {
				return result;
			}
			if (this.isCursorHeader()) {
				return result;
			}
			if (this.cursor == null) {
				throw new Exception("链表结构异常，其中游标为空。");
			}
			this.cursor = this.cursor.prevChain();
			result = this.cursor;
		} catch (Exception e) {
			logger.error("执行 prevCursor 异常，错误信息：" + e.getMessage()); 
		} 
		return result;
	}

	@Override
	public boolean isCursorTail() {
		return this.isEmpty()?true:(this.cursor.nextChain() == null);
	}

	@Override
	public boolean isCursorHeader() {
		return this.isEmpty()?true:(this.cursor.prevChain() == null);
	}

	@SuppressWarnings("unused")
	@Override
	public IChainNodeAble<T> appendChainNode(IChainNodeAble<T> _new_chain_node) {
		IChainNodeAble<T> result = null;
		try {
			if (_new_chain_node == null) {
				throw new Exception("入口参数 _new_chain_node 没有指定！");
			}
			if (this.isEmpty()) {
				this.header = _new_chain_node;
				this.tail = _new_chain_node;
				this.cursor = _new_chain_node;
				return this.cursor;
			}

			result = this.cursor;
			if (result == null) {
				throw new RuntimeException("实例化AZChain 失败！");
			}
			/* 将新成员加入链表 */
			_new_chain_node.putNext(result.nextChain());
			if (result.nextChain() != null) {
				result.nextChain().putPrev(_new_chain_node);
			}

			result.putNext(_new_chain_node);
			_new_chain_node.putPrev(result);
			/* 移动游标在新的链表成员 */
			this.cursor = _new_chain_node;
			
			if ( this.cursor.prevChain() == null ) {
				this.header = this.cursor;
			}
			if (this.cursor.nextChain() == null ) {
				this.tail = this.cursor;
			}
			result = this.cursor;
			//this.size++;
		} catch (Exception e) {
			logger.error("执行 appendChainNode 异常，错误信息：" + e.getMessage());
		}
		return result;
	}

	@Override
	public IChainNodeAble<T> appendChainNode(long _post, IChainNodeAble<T> _new_chain_node) {
		IChainNodeAble<T> result = null;
		try {
			IChainNodeAble<T> _cursor = this.skip(_post);
			if (_cursor == null) {
				throw new Exception("跳转出现异常：出现空的指针！");
			}
			/* 在指定位置后，添加 */
			result = this.appendChainNode(_new_chain_node);
		} catch (Exception e) {
			logger.error("执行 appendChainNode POST 异常，错误信息：" + e.getMessage());
		}
		return result;
	}

	@Override
	public IChainNodeAble<T> appendTailChainNode(IChainNodeAble<T> _new_chain_node) {
		IChainNodeAble<T> result = null;
		try {
			this.end();
			/* 在指定位置后，添加 */
			result = this.appendChainNode(_new_chain_node);
		} catch (Exception e) {
			logger.error("执行 appendTailChainNode 异常，错误信息：" + e.getMessage());
		}
		return result;
	}

	@Override
	public IChainNodeAble<T> appendHeaderChainNode(IChainNodeAble<T> _new_chain_node) {
		IChainNodeAble<T> result = null;
		try {
			this.first();
			/* 在指定位置后，添加 */
			result = this.appendChainNode(_new_chain_node);
		} catch (Exception e) {
			logger.error("执行 appendTailChainNode 异常，错误信息：" + e.getMessage());
		}
		return result;
	}

	@Override
	public IChainNodeAble<T> insertChainNode(IChainNodeAble<T> _new_chain_node) {
		IChainNodeAble<T> result = null;
		try {
			if (_new_chain_node == null) {
				throw new Exception("入口参数 _new_chian_data 没有指定！");
			}
 
			if (this.isEmpty()) {
				this.header = _new_chain_node;
				this.tail = _new_chain_node;
				this.cursor = _new_chain_node;
				return this.cursor;
			}

			if (this.cursor == null) {
				throw new Exception("链表为空或者游标没有指定！");
			}

			if (this.cursor.prevChain() != null) {
				this.cursor.prevChain().putNext(_new_chain_node);
			}
			_new_chain_node.putPrev(this.cursor.prevChain());

			_new_chain_node.putNext(this.cursor);
			this.cursor.putPrev(_new_chain_node);
			this.cursor = _new_chain_node;
			
			if ( this.cursor.prevChain() == null ) {
				this.header = this.cursor;
			}
			if (this.cursor.nextChain() == null ) {
				this.tail = this.cursor;
			}
			result = this.cursor;
		} catch (Exception e) {
			logger.error("执行 insertChainNode 异常，错误信息：" + e.getMessage());
		}
		return result;
	}

	@Override
	public IChainNodeAble<T> insertChainNode(long _post, IChainNodeAble<T> _new_chain_node) {
		IChainNodeAble<T> result = null;
		try {
			IChainNodeAble<T> _cursor = this.skip(_post);
			if (_cursor == null) {
				throw new Exception("跳转出现异常：出现空的指针！");
			}
			/* 在指定位置后，添加 */
			result = this.insertChainNode(_new_chain_node);
		} catch (Exception e) {
			logger.error("执行 insertChainNode POST 异常，错误信息：" + e.getMessage());
		}
		return result;
	}

	@Override
	public IChainNodeAble<T> insertHeaderChainNode(IChainNodeAble<T> _new_chain_node) {
		IChainNodeAble<T> result = null;
		try {
			this.first();
			/* 在指定位置后，添加 */
			result = this.insertChainNode(_new_chain_node);
		} catch (Exception e) {
			logger.error("执行 insertHeaderChainNode 异常，错误信息：" + e.getMessage());
		}
		return result;
	}

	@Override
	public IChainNodeAble<T> insertTailHeaderChainNode(IChainNodeAble<T> _new_chain_node) {
		IChainNodeAble<T> result = null;
		try {
			this.end();
			/* 在指定位置后，添加 */
			result = this.insertChainNode(_new_chain_node);
		} catch (Exception e) {
			logger.error("执行 insertTailHeaderChainNode 异常，错误信息：" + e.getMessage());
		}
		return result;
	}

	public void stop() {
		
	}
	
	public boolean isFixed() {
		boolean result = false;
		try {
			result = true;
		} catch (Exception e) {
			logger.error(null);
		}
		return result;
	}
	
	public void execute(Object sender) {
		
	}
	
	public boolean cycle() {
		return false;
	}
	
}
